//
//  RTJMyBankListController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/13.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJMyBankListController.h"
#import "RTJUserInfoCommntCell.h"
#import "RTJBindPayCardController.h"

@interface RTJMyBankListController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UILabel *bankName;
@property (weak, nonatomic) IBOutlet UILabel *bankNum;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTop;
@property (nonatomic,strong) NSMutableArray *data;

@end

@implementation RTJMyBankListController

-(NSMutableArray *)data {
    if (!_data) {
        
        _data = [NSMutableArray array];
    }
    return _data;
}

-(void)viewWillAppear:(BOOL)animated{
    
    [self userbanklist];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"银行卡";
    
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight=300;
    
     self.viewTop.constant = screenH == 812 ? 88 : 64;
    UIButton *clear=[UIButton buttonWithType:UIButtonTypeSystem];
    clear.size=CGSizeMake(60, 30);
    [clear setTitle:@"查看限额" forState:UIControlStateNormal];
    [clear setTitleColor:[UIColor colorForHex:@"#666666"] forState:UIControlStateNormal];
    clear.titleLabel.font=[UIFont systemFontOfSize:14];;
    [clear addTarget:self action:@selector(chakan) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:clear];
    
}

-(void)userbanklist{
    
    [SVProgressHUD showWithStatus:nil];
    [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
    
    [RTJNetWork asyncRequestWithURL:@"user/bank_list" params:@{@"is_quick":@"0"} method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        [self.view removeEmptyView];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                NSArray *arr = data[@"data"];
                
                [self.data removeAllObjects];
                [self.data addObjectsFromArray:arr];
                
                [self.tableView reloadData];

            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                [self.view showEmptyViewWithStr:data[@"msg"] image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self userbanklist];
                }];
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self userbanklist];
            }];
        }
        
    }];
    
}

-(void)chakan{
    
    RTJCommentController *vc = [[RTJCommentController alloc]init];
    vc.title = @"查看限额";
    vc.imgName = @"ckxe";
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _data.count+1;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJUserInfoCommntCell";
    RTJUserInfoCommntCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
        
        if (indexPath.row == _data.count) {
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"RTJUserInfoCommntCell" owner:self options:nil][7];

        }else{
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"RTJUserInfoCommntCell" owner:self options:nil][6];

            cell.userBankName.text = _data[indexPath.row][@"name"];
            cell.userBankNum.text = _data[indexPath.row][@"bankcard_format"];
            [cell.jiechuBtn addTarget:self action:@selector(jiechu:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == _data.count) {
        
        RTJBindPayCardController *vc = [[RTJBindPayCardController alloc]init];
        vc.bankListArr = _data;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
}

-(void)jiechu:(UIButton *)btn{
    
    UIView *superview = [btn superview];//获取父类view
    RTJUserInfoCommntCell *cell = (RTJUserInfoCommntCell *)[superview superview];//获取cell
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];//获取cell对应的section
    
    NSString *ids = [NSString stringWithFormat:@"%@",_data[indexPath.row][@"id"]];
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否解除绑定此银行卡？" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [RTJNetWork asyncRequestWithURL:@"user/unbind_bank" params:@{@"bank_id":ids} method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
            
            [SVProgressHUD dismiss];
            if (success) {
                
                if ([data[@"status"]integerValue] == 1) {
                    
                    [SVProgressHUD showSuccessWithStatus:@"解除绑定成功"];
                    [self.data removeObjectAtIndex:indexPath.row];
                    [self.tableView reloadData];
                    
                }else{
                    
                    
                    [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"解除绑定失败:%@",data[@"msg"]]];
                    
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
                
            }
            
        }];
      
        
    }]];
    
    [self presentViewController:alertController animated:YES completion:nil];
    

}

@end
